home *** CD-ROM | disk | FTP | other *** search
/ Amiga Plus 1999 #2 / Amiga Plus CD - 1999 - No. 2.iso / System-Boost / Workbench / ToolManager / Source / Library / commodities.c < prev    next >
C/C++ Source or Header  |  1998-06-17  |  6KB  |  275 lines

  1. /*
  2.  * commodities.c  V3.1
  3.  *
  4.  * ToolManager commodities handling routines
  5.  *
  6.  * Copyright (C) 1990-98 Stefan Becker
  7.  *
  8.  * This source code is for educational purposes only. You may study it
  9.  * and copy ideas or algorithms from it for your own projects. It is
  10.  * not allowed to use any of the source codes (in full or in parts)
  11.  * in other programs. Especially it is not allowed to create variants
  12.  * of ToolManager or ToolManager-like programs from this source code.
  13.  *
  14.  */
  15.  
  16. #include "toolmanager.h"
  17.  
  18. /* Global data */
  19. const char ToolManagerName[] = "ToolManager";
  20.  
  21. /* Local data */
  22. static struct Library   *CxBase;
  23. static struct MsgPort   *BrokerPort;
  24. static struct CxObj     *Broker;
  25. static struct NewBroker  BrokerData = {
  26.  NB_VERSION, ToolManagerName,
  27.  "ToolManager V" TMVERSION " © " TMCOPYRIGHTYEAR " Stefan Becker", NULL,
  28.  NBU_UNIQUE, 0, 0, NULL, 0
  29. };
  30.  
  31. /* Activate commodities */
  32. #define DEBUGFUNCTION StartCommodities
  33. LONG StartCommodities(void)
  34. {
  35.  LONG rc = -1;
  36.  
  37.  COMMODITIES_LOG(LOG0(Entry))
  38.  
  39.  /* Open commodities.library */
  40.  if (CxBase = OpenLibrary("commodities.library", 39)) {
  41.  
  42.   COMMODITIES_LOG(LOG1(Library, "0x%08lx", CxBase))
  43.  
  44.   /* Create broker port */
  45.   if (BrokerPort = CreateMsgPort()) {
  46.  
  47.    COMMODITIES_LOG(LOG1(Port, "0x%08lx", BrokerPort))
  48.  
  49.    /* Set broker port */
  50.    BrokerData.nb_Port = BrokerPort;
  51.  
  52.    /* Localize broker description */
  53.    BrokerData.nb_Descr = TranslateString(LOCALE_LIBRARY_COMMODITIES_STR,
  54.                                          LOCALE_LIBRARY_COMMODITIES);
  55.  
  56.    /* Create broker */
  57.    if (Broker = CxBroker(&BrokerData, NULL)) {
  58.  
  59.     COMMODITIES_LOG(LOG1(Broker, "0x%08lx", Broker))
  60.  
  61.     /* Activate broker */
  62.     ActivateCxObj(Broker, TRUE);
  63.  
  64.     /* Return port signal */
  65.     rc = BrokerPort->mp_SigBit;
  66.  
  67.    } else
  68.     DeleteMsgPort(BrokerPort);
  69.   }
  70.  
  71.   if (rc == -1) CloseLibrary(CxBase);
  72.  }
  73.  
  74.  COMMODITIES_LOG(LOG1(Result, "%ld", rc))
  75.  
  76.  return(rc);
  77. }
  78.  
  79. /* Stop Commodities */
  80. #undef  DEBUGFUNCTION
  81. #define DEBUGFUNCTION StopCommodities
  82. void StopCommodities(void)
  83. {
  84.  COMMODITIES_LOG(LOG3(Entry, "Broker 0x%08lx Port 0x%08lx CxBase 0x%08lx",
  85.                       Broker, BrokerPort, CxBase))
  86.  
  87.  /* Deactivate broker */
  88.  ActivateCxObj(Broker, FALSE);
  89.  
  90.  /* Delete commodities resources */
  91.  DeleteCxObjAll(Broker);
  92.  SafeDeleteMsgPort(BrokerPort);
  93.  CloseLibrary(CxBase);
  94. }
  95.  
  96. /* Handle commodities event */
  97. #undef  DEBUGFUNCTION
  98. #define DEBUGFUNCTION HandleCommodities
  99. void HandleCommodities(void)
  100. {
  101.  CxMsg *msg;
  102.  
  103.  COMMODITIES_LOG(LOG0(Entry))
  104.  
  105.  /* Empty message queue */
  106.  while (msg = (CxMsg *) GetMsg(BrokerPort)) {
  107.  
  108.   COMMODITIES_LOG(LOG1(Event, "0x%08lx", CxMsgType(msg)))
  109.  
  110.   /* What type of Commodities event? */
  111.   switch (CxMsgType(msg)) {
  112.    case CXM_IEVENT:
  113.  
  114.     COMMODITIES_LOG(LOG1(Activating, "0x%08lx", CxMsgID(msg)))
  115.  
  116.     DoMethod((Object *) CxMsgID(msg), TMM_Activate, NULL);
  117.     break;
  118.  
  119.    case CXM_COMMAND:
  120.  
  121.     /* Which commodities command? */
  122.     switch (CxMsgID(msg)) {
  123.      case CXCMD_DISABLE: ActivateCxObj(Broker, FALSE); break;
  124.      case CXCMD_ENABLE:  ActivateCxObj(Broker, TRUE);  break;
  125.      case CXCMD_KILL:    KillToolManager();            break;
  126.      }
  127.     break;
  128.   }
  129.  
  130.   /* Reply message */
  131.   ReplyMsg((struct Message *) msg);
  132.  }
  133. }
  134.  
  135. /* Create a commodities hotkey */
  136. #undef  DEBUGFUNCTION
  137. #define DEBUGFUNCTION CreateHotKey
  138. CxObj *CreateHotKey(const char *desc, Object *obj)
  139. {
  140.  CxObj *filter;
  141.  CxObj *rc     = NULL;
  142.  
  143.  COMMODITIES_LOG(LOG3(Arguments, "Description '%s' (0x%08lx) Object 0x%08lx",
  144.                       desc, desc, obj))
  145.  
  146.  /* Create dummy filter object */
  147.  if (filter = CxFilter(desc)) {
  148.   CxObj *sender;
  149.  
  150.   COMMODITIES_LOG(LOG1(Filter, "0x%08lx", filter))
  151.  
  152.   /* Create sender object */
  153.   if (sender = CxSender(BrokerPort, obj)) {
  154.    CxObj *translator;
  155.  
  156.    COMMODITIES_LOG(LOG1(Sender, "0x%08lx", sender))
  157.  
  158.    /* Attach sender to filter */
  159.    AttachCxObj(filter, sender);
  160.  
  161.    /* Create a black hole translation object */
  162.    if (translator = CxTranslate(NULL)) {
  163.  
  164.     COMMODITIES_LOG(LOG1(Translator, "0x%08lx", translator))
  165.  
  166.     /* Attach translator to filter */
  167.     AttachCxObj(filter, translator);
  168.  
  169.     COMMODITIES_LOG(LOG1(Cx Error, "0x%08lx", CxObjError(filter)))
  170.  
  171.     /* Got a Commodities error? */
  172.     if (CxObjError(filter) == 0) {
  173.  
  174.      /* Attach object to broker */
  175.      AttachCxObj(Broker, filter);
  176.  
  177.      /* All OK */
  178.      rc = filter;
  179.     }
  180.    }
  181.   }
  182.  
  183.   /* Delete all CxObjects */
  184.   if (rc == NULL) DeleteCxObjAll(filter);
  185.  }
  186.  
  187.  COMMODITIES_LOG(LOG1(Result, "0x%08lx", rc))
  188.  
  189.  return(rc);
  190. }
  191.  
  192. /* Delete a CxObject and remove all associated messages from the broker port */
  193. #undef  DEBUGFUNCTION
  194. #define DEBUGFUNCTION SafeDeleteCxObjAll
  195. void SafeDeleteCxObjAll(struct CxObj *cxobj, Object *obj)
  196. {
  197.  CxMsg *msg;
  198.  
  199.  COMMODITIES_LOG(LOG2(Arguments, "CxObj 0x%08lx Object 0x%08lx", cxobj, obj))
  200.  
  201.  /* Delete commodities object */
  202.  DeleteCxObjAll(cxobj);
  203.  
  204.  /* Disable multi-tasking */
  205.  Forbid();
  206.  
  207.  /* Scan message port list */
  208.  msg = (CxMsg *) GetHead((struct MinList *) &BrokerPort->mp_MsgList);
  209.  while (msg) {
  210.   CxMsg *nextmsg = (CxMsg *) GetSucc((struct MinNode *) msg);
  211.  
  212.   /* Does the message point to this object? */
  213.   if ((Object *) CxMsgID(msg) == obj) {
  214.  
  215.    /* Remove it from list */
  216.    Remove((struct Node *) msg);
  217.  
  218.    /* Reply it */
  219.    ReplyMsg((struct Message *) msg);
  220.   }
  221.  
  222.   /* Next message */
  223.   msg = nextmsg;
  224.  }
  225.  
  226.  /* Enable multi-tasking */
  227.  Permit();
  228. }
  229.  
  230. /* Create and send an input event */
  231. #undef  DEBUGFUNCTION
  232. #define DEBUGFUNCTION SendInputEvent
  233. BOOL SendInputEvent(const char *desc)
  234. {
  235.  struct InputEvent *ie;
  236.  BOOL               rc = FALSE;
  237.  
  238.  COMMODITIES_LOG(LOG2(Entry, "%s (0x%08lx)", desc, desc))
  239.  
  240.  /* Allocate memory for input event */
  241.  if (ie = GetMemory(sizeof(struct InputEvent))) {
  242.   static struct InputXpression  ParseBuffer = {IX_VERSION};
  243.  
  244.   COMMODITIES_LOG(LOG1(Event, "0x%08lx", ie))
  245.  
  246.   /* Parse description string */
  247.   if (ParseIX(desc, &ParseBuffer) == 0) {
  248.  
  249.    COMMODITIES_LOG(LOG0(Parsed OK))
  250.  
  251.    /* Description OK, initialize input event */
  252.    ie->ie_NextEvent    = NULL;
  253.    ie->ie_Class        = ParseBuffer.ix_Class;
  254.    ie->ie_SubClass     = 0;
  255.    ie->ie_Code         = ParseBuffer.ix_Code;
  256.    ie->ie_Qualifier    = ParseBuffer.ix_Qualifier;
  257.    ie->ie_EventAddress = NULL;
  258.  
  259.    /* Set time stamp */
  260.    CurrentTime(&ie->ie_TimeStamp.tv_secs, &ie->ie_TimeStamp.tv_micro);
  261.  
  262.    /* Enqueue event */
  263.    AddIEvents(ie);
  264.  
  265.    /* All OK */
  266.    rc = TRUE;
  267.   }
  268.  
  269.   /* Free input event */
  270.   FreeMemory(ie, sizeof(struct InputEvent));
  271.  }
  272.  
  273.  return(rc);
  274. }
  275.